package com.walle.sonar.export.report.api.issues;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jayway.jsonpath.ReadContext;
import com.jayway.jsonpath.TypeRef;
import com.walle.ak47.commons.utils.ConvertUtils;
import com.walle.ak47.commons.utils.HttpClientPool;
import com.walle.ak47.commons.utils.PageUtils;
import com.walle.ak47.commons.utils.StringUtils;
import com.walle.ak47.commons.utils._JsonPath;
import com.walle.ak47.commons.utils.json.GBuilder;
import com.walle.sonar.export.report.api.lines.GetLineInfo;
import com.walle.sonar.export.report.api.rules.GetRules;
import com.walle.sonar.export.report.api.rules.RuleInfo;
import com.walle.sonar.export.report.reportdoc.Config;

public class GetIssueInfos {

//	String issues_url = "http://127.0.0.1:9000/api/issues/search?s=FILE_LINE&resolved=false&types=BUG,CODE_SMELL,VULNERABILITY&ps=500&facets=severities,types&additionalFields=_all" ;
	static String get_all_issues_url = "http://127.0.0.1:9000/api/issues/search?s=FILE_LINE&resolved=false&ps=500&facets=severities,types&additionalFields=_all"+Config._type ;
	static String get_issues_by_pageNum_url = (get_all_issues_url+"&p=") ;
	
	/**
	 * 获取所有问题列表
	 * @return
	 */
	public static List<IssueInfo> getAllIssues() {
		
		List<IssueInfo> issueInfoAll = new ArrayList<>() ;
		
		HashMap<String, Object> _headers = new HashMap<String, Object>();
		String result = HttpClientPool.httpGet(get_all_issues_url, _headers);
		
		ReadContext parse = _JsonPath.parse(result);
		Integer total = _JsonPath.read(parse, "$.total", new TypeRef<Integer>() {}  );
		int pageTotal = PageUtils.getTotalPage(total, 500) ;
		for (int i=1;i<=pageTotal;i++) {
			List<IssueInfo> issues = getIssuesByPageNum(i);
			issueInfoAll.addAll(issues) ;
		}
		
		return issueInfoAll ;
	}
	
	private static List<IssueInfo> getIssuesByPageNum(int p) {
	
		 Map<String, RuleInfo> ruleInfosForMap = GetRules.getRuleInfosForMap();
		
		HashMap<String, Object> _headers = new HashMap<String, Object>();
		String result = HttpClientPool.httpGet( (get_issues_by_pageNum_url+p) , _headers);
		
		ReadContext parse = _JsonPath.parse(result);
		List<HashMap<String, Object>> read = _JsonPath.read(parse, "$.issues", new TypeRef<List<HashMap<String, Object>>>() {}  );
		List<IssueInfo> issueInfos = new ArrayList<IssueInfo>() ;
		if ( read != null && !read.isEmpty() ) {
			read.forEach(issue->{
				
				try
				{
					IssueInfo issueInfo = new IssueInfo() ;
					issueInfo.setKey( issue.get("key").toString() );
					issueInfo.setType(issue.get("type").toString());
					issueInfo.setComponent(issue.get("component").toString());
					issueInfo.setRule(issue.get("rule").toString());
					issueInfo.setSeverity( issue.get("severity").toString() );
					issueInfo.setShortDesc( ruleInfosForMap.get(issue.get("rule").toString()).getMdNote());
					issueInfo.setMdDesc( ruleInfosForMap.get(issue.get("rule").toString()).getMdDesc() );
					if (issue.get("line")!=null && StringUtils.isNotBlank(issue.get("line").toString()))
						issueInfo.setLine(issue.get("line").toString());
					
					GetLineInfo getLineInfo = new GetLineInfo() ;
					String codeLine = getLineInfo.getCodeLine( issue.get("component").toString(), ConvertUtils.toInt(issue.get("line").toString()));
					issueInfo.setLineCode(codeLine);
					issueInfos.add(issueInfo);
				}
				catch(Throwable err)
				{
					err.printStackTrace();
					System.err.println(GBuilder.toJson(issue));
				}
			});
		}
		
		return issueInfos ;
	}
	
	
	public static void main(String[] args) {
		List<IssueInfo> issues = GetIssueInfos.getAllIssues();
		System.err.println(issues);
	}
}
